
def calc_Iion(V, dt, Iinj, C):
    '''
    Calculate the ionic current given the voltage trace and the capacitance.
                    dV
    Iion = Iinj - C----
                    dt
    as seen in  Badel et. al. 2008 Biological Cybernetics "Extracting non-linear integrate-and-fire models from experimental data using dynamic I-V curves." 
    Inputs:
        V       : a 1-d numpy array representing the voltage sampled at discrete time intervals
                  dt.
        dt      : the descrete time intervals
        Iinj    : a 1-d numpy array representing the injected current at the same time intervals
                  as the input V.
        C       : the capacitance of the neuron in appropriate units such that all terms in
                  the above equation have the same units (amps, or na or whatever)
    Returns:
        Iion    : a 1-d numpy array which corresponds to a first order aproximation of the ionic current
                  through the neuron's cell membrane.
    '''
    from genutils.signal_derivative import signal_derivative_1D as derivative
    # take the first time derivative of the voltage.
    dVdt = derivative(V, dt)
    return Iinj - C*dVdt
    
